package com.kotasoft.training.roogwteclipselink.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

import com.kotasoft.training.roogwteclipselink.client.scaffold.place.ProxyPlace;
import com.kotasoft.training.roogwteclipselink.client.scaffold.place.ProxyListPlace;
import com.kotasoft.training.roogwteclipselink.client.scaffold.place.ProxyEditView;
import com.kotasoft.training.roogwteclipselink.client.managed.request.AsignaturaProxy;
import com.kotasoft.training.roogwteclipselink.client.managed.request.DepartamentoProxy;
import java.util.Set;
import com.kotasoft.training.roogwteclipselink.client.managed.request.ProfesorProxy;
import com.kotasoft.training.roogwteclipselink.client.managed.ui.ProfesorSetEditor;
import com.kotasoft.training.roogwteclipselink.client.managed.request.ApplicationRequestFactory;
import com.kotasoft.training.roogwteclipselink.client.scaffold.activity.IsScaffoldMobileActivity;


import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

/**
 * Wraps AsignaturaProxy edit and create activities to manage extra portions of their
 * views, like value pickers.
 */
public class AsignaturaEditActivityWrapper implements Activity,
    IsScaffoldMobileActivity {

  /**
   * The view used by this activity.
   * 
   * @param <V> the type of the ProxyEditView
   */
  public interface View<V extends ProxyEditView<AsignaturaProxy, V>>
      extends ProxyEditView<AsignaturaProxy, V> {
    
    
    void setDepartamentoPickerValues(Collection<DepartamentoProxy> values);
    
    void setProfesoresPickerValues(Collection<ProfesorProxy> values);
    
  }

  private final EntityProxyId<AsignaturaProxy> proxyId;
  private final ApplicationRequestFactory requests;
  private final View<?> view;
  private final Activity wrapped;

  public AsignaturaEditActivityWrapper(ApplicationRequestFactory requests,
        View<?> view, Activity activity, EntityProxyId<AsignaturaProxy> proxyId) {
    this.requests = requests;
    this.view = view;
    this.wrapped = activity;
    this.proxyId = proxyId;
  }

  public Place getBackButtonPlace() {
    return (proxyId == null) ? new ProxyListPlace(AsignaturaProxy.class) :
      new ProxyPlace(proxyId, ProxyPlace.Operation.DETAILS);
  }

  public String getBackButtonText() {
    return "Cancel";
  }

  public Place getEditButtonPlace() {
    return null;
  }

  public String getTitleText() {
    return (proxyId == null) ? "New Asignatura" : "Edit Asignatura";
  }

  public boolean hasEditButton() {
    return false;
  }

  @Override
  public String mayStop() {
    return wrapped.mayStop();
  }

  @Override
  public void onCancel() {
    wrapped.onCancel();
  }

  @Override
  public void onStop() {
    wrapped.onStop();
  }

  @Override
  public void start(AcceptsOneWidget display, EventBus eventBus) {

    view.setDepartamentoPickerValues(Collections.<DepartamentoProxy> emptyList());
    requests.departamentoRequest().findDepartamentoEntries(0, 50).with(
        com.kotasoft.training.roogwteclipselink.client.managed.ui.DepartamentoProxyRenderer.instance().getPaths()).fire(
            new Receiver<List<DepartamentoProxy>>() {
                public void onSuccess(List<DepartamentoProxy> response) {
                    List<DepartamentoProxy> values = new ArrayList<DepartamentoProxy>();
                    values.add(null);
                    values.addAll(response);
                    view.setDepartamentoPickerValues(values);
            }
    });
    view.setProfesoresPickerValues(Collections.<ProfesorProxy> emptyList());
    requests.profesorRequest().findProfesorEntries(0, 50).with(
        com.kotasoft.training.roogwteclipselink.client.managed.ui.ProfesorProxyRenderer.instance().getPaths()).fire(
            new Receiver<List<ProfesorProxy>>() {
                public void onSuccess(List<ProfesorProxy> response) {
                    List<ProfesorProxy> values = new ArrayList<ProfesorProxy>();
                    values.add(null);
                    values.addAll(response);
                    view.setProfesoresPickerValues(values);
            }
    });
    
    wrapped.start(display, eventBus);
  }
}
